<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户信息表格</title>
    <link rel="stylesheet" href="../css/users.css">
</head>

<body>
    <div class="container">
        <h1>用户信息表格</h1>
        <table>
            <thead>
                <tr>
                    <th>编号</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>邮箱</th>
                    <th>注册时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>

                {% for user in users %}
                <tr class="user-item">
                    <td>{{user.id}}</td>
                    <td>{{user.userName}}</td>
                    <td>{{user.gender}}</td>
                    <td>{{user.age}}</td>
                    <td>{{user.email}}</td>
                    <td>{{user.registrationTime}}</td>
                    <td>
                        <input class="update-btn" type="button" value="修改" onclick="userEdit(event)">
                        <input class="delete-btn" type="button" value="删除" onclick="userDel(`{{user.id}}`)">
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        <div class="table-controls">
            <div class="pagination">
                <button onclick="prevPage()">上一页</button>
                <span>第 1 页</span>
                <button onclick="nextPage()">下一页</button>
            </div>
            <div class="buttons">
                <button onclick="addUser()">添加学生</button>
                <button onclick="goHome()">返回首页</button>
            </div>
        </div>
    </div>
    <button class="open-popup-btn" onclick="openPopup()">打开导航</button>

    <!-- 弹窗背景遮罩 -->
    <div id="overlay" class="overlay"></div>
    <!-- 弹窗内容 -->
    <div id="popup" class="popup">
        <h2>导航弹窗</h2>
        <a href="http://127.0.0.1:8000">首页</a>
        <button onclick="closePopup()">关闭</button>
    </div>

    <script>
        function userEdit(event) {
            const editBtn = event.target;


            // 从data属性获取用户ID（推荐方式）
            const userId = parseInt(editBtn.dataset.userId);

            // 验证ID有效性
            if (!userId || isNaN(userId)) {
                console.error('无效的用户ID');
                return;
            }

            if (!confirm('确定要修改该用户信息吗？')) {
                return;
            }

            location.href = `/users/${userId}/adAndUp`;
        }

        function userDel(userId) {
            console.log(userId);


            // 验证ID有效性
            if (!userId || isNaN(userId)) {
                console.error('无效的用户ID');
                return;
            }

            fetch(`/users/${userId}`, {
                method: "DELETE",
            })
                .then(response => {
                    if (response.status === 200) {
                       console.log("删除成功");
                       
                    }else{
                        throw new Error(`删除失败，状态码：${response.status}`);
                    }
                })
                .catch(error => {
                    console.error("删除出错:", error);
                    alert('删除失败，请重试');
                });
        }

        function addUser() {
            location.href = "/users/0";
        }
        function goHome() {
            location.href = "/users"
        }
        // Popup
        function openPopup() {
            document.getElementById('overlay').style.display = 'flex';
            document.getElementById('popup').style.display = 'block';
        }
        function closePopup() {
            document.getElementById('overlay').style.display = 'none';
            document.getElementById('popup').style.display = 'none';
        }
        // Page
        function prevPage() {
            alert('上一页');
        }
        function nextPage() {
            alert('下一页');
        }
    </script>
</body>

</html>